<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="报修单位" prop="repairUnit">
        <el-input
            v-model="queryParams.repairUnit"
            placeholder="请输入报修单位"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
            v-model="queryParams.orderNo"
            placeholder="请输入订单编号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择订单类型" clearable style="width: 200px">
          <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            size="small"
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:notice:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" align="center" prop="index" width="60" type="index"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" width="150" show-overflow-tooltip/>
      <el-table-column label="报修单位" align="center" prop="repairUnit" min-width="140" show-overflow-tooltip/>
      <el-table-column label="报修设备" align="center" prop="equipment" min-width="120" show-overflow-tooltip/>
      <el-table-column label="品牌" align="center" prop="brand" width="100"/>
      <el-table-column label="报修人" align="center" prop="customerName" width="100" show-overflow-tooltip/>
      <el-table-column label="订单状态" align="center" prop="type" width="100">
        <template #default="scope">
          <el-tag type="primary">{{
              scope.row.type === 1 ? '已报修' : scope.row.type === 2 ? '已接单' : scope.row.type === 3 ? '已预约' : scope.row.type === 4 ? '已到达' : scope.row.type === 5 ? '待评价' : '维修完成'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="phone" width="120"/>
      <el-table-column label="问题描述" align="center" prop="description" min-width="100" show-overflow-tooltip/>
      <el-table-column fixed="right" label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button v-if="scope.row.type == 3 || scope.row.type == 5" link type="primary" icon="Edit" @click="handleToMake(scope.row,'订单处理')" size="small">操作</el-button>
          <el-button v-if="scope.row.type == 1" link type="primary" icon="Edit" @click="handleToMake(scope.row,'订单分派')" size="small">操作</el-button>
          <el-button link @click="handleDetail(scope.row)" size="small">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="100%" fullscreen append-to-body>
      <!-- 详情弹窗 -->
      <Details :OrderNo="OrderNo" ref="detail" v-if="title==='订单详情'"></Details>
      <!--订单处理-->
      <Assignment :OrderNo="OrderNo" ref="assignment" v-else-if="title==='订单分派'"></Assignment>
      <!--订单处理-->
      <Offer :OrderNo="OrderNo" ref="offer" v-else></Offer>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Notice">
import {confirmationOrder, dealOrder, getListOrders, saveOrderPart} from "@/api/system/orders.js"
import Details from './components/Details.vue'
import Assignment from './components/Assignment.vue'
import Offer from './components/Offer.vue'
import { nextTick } from 'vue';

const {proxy} = getCurrentInstance()

const noticeList = ref([])
const OrderDetail = ref({})//订单详情
const OrderNo = ref('')//订单详情
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const title = ref("")

const detail = ref(null)
const assignment = ref(null)
const offer = ref(null)


const data = reactive({
  typeList: [
    {
      label: '待受理',
      value: 1
    }, {
      label: '待上门',
      value: 2
    }, {
      label: '待报价',
      value: 3
    }, {
      label: '待维修',
      value: 4
    }, {
      label: '待评价',
      value: 5
    }, {
      label: '已完成',
      value: 6
    },
  ],
  queryParams: {
    page: 1,
    pageSize: 10,
    repairUnit: undefined,
    orderNo: undefined,
    type: undefined
  },
  /*form: {},
  rules: {
    noticeTitle: [{required: true, message: "公告标题不能为空", trigger: "blur"}],
    noticeType: [{required: true, message: "公告类型不能为空", trigger: "change"}]
  },*/
})

const {queryParams, typeList} = toRefs(data)


/**修改按钮操作 */
function handleToMake(row,str) {
  title.value = str
  OrderNo.value = row.orderNo
  open.value = true
  if(str==='订单处理'){
    nextTick(() => {
      offer.value.getDetail(row.orderNo,row.type)
    });
  }else{
    nextTick(() => {
      assignment.value.getDetail(row.orderNo)
    });
  }
}

/** 提交按钮 */
function submitForm() {
  if(title.value==='订单分派'){
    // let type = assignment.value.value == 1 ? assignment.value.baseInfo.type*1+1 : 6
    let queryParams = reactive({
      damageReason:assignment.value.phoneDo.handle_input1,//损坏原因
      dealType:assignment.value.value,//处理方式 1 订单分派 2 电话处理 3 关闭订单
      isDispatch:assignment.value.value==1?true:false,//是否分派
      isItem:assignment.value.radio==1?true:false,//是否项目
      isPay:assignment.value.project.isPay,//是否需要付费
      itemId:assignment.value.project.id,//项目ID
      jobAnalysis:assignment.value.phoneDo.handle_input2,//工作分析
      orderId:assignment.value.baseInfo.id,//订单id
      orderNo:assignment.value.baseInfo.orderNo,//订单编号
      remark:assignment.value.phoneDo.handle_input3,//备注
      // type:type,//订单状态
      workId:assignment.value.works.id,//维修人员ID
      dropInFee:assignment.value.works.dropInFee,//上门检查费
    })
    console.log(queryParams)
    dealOrder(queryParams).then(res=>{
      if(res.code === 200){
        proxy.$modal.msgSuccess(res.msg)
        open.value = false
        getList()
      }
    })
  }else if(title.value==='订单处理'){
    if(offer.value.baseInfo.type == 3){
      let orderPartDTOS = offer.value.accesses
      orderPartDTOS.forEach(item=>{
        item.orderId = offer.value.baseInfo.id
        item.id = offer.value.baseInfo.id
        item.repairFee = offer.value.rgcb*1
      })
      saveOrderPart(orderPartDTOS).then(res=>{
        if(res.code === 200){
          proxy.$modal.msgSuccess(res.msg)
          open.value = false
          getList()
        }
      })
    }else if(offer.value.baseInfo.type == 5){
      let orderConfirmationDTO = {
        orderParts:offer.value.accesses,
        actFee:offer.value.cbzj,
        dropInFee:offer.value.deposit,
        orderId:offer.value.baseInfo.id,
        orderNo:offer.value.baseInfo.orderNo,
        repairFee:offer.value.repairFee,
        serviceScore:offer.value.appraise.serviceScore,
        serviceComment:offer.value.appraise.serviceComment,
        repairScore:offer.value.appraise.repairScore,
        repairComment:offer.value.appraise.repairComment,
      }
      console.log(orderConfirmationDTO)
      confirmationOrder(orderConfirmationDTO).then(res=>{
        if(res.code === 200){
          proxy.$modal.msgSuccess(res.msg)
          open.value = false
          getList()
        }
      })
    }else{
      let orderConfirmationDTO = {
        orderParts:offer.value.accesses,
        actFee:offer.value.cbzj,
        dropInFee:offer.value.deposit,
        orderId:offer.value.baseInfo.id,
        orderNo:offer.value.baseInfo.orderNo,
        repairFee:offer.value.rgcb*1,
        serviceScore:offer.value.appraise.serviceScore,
        serviceComment:offer.value.appraise.serviceComment,
        repairScore:offer.value.appraise.repairScore,
        repairComment:offer.value.appraise.repairComment,
      }
      console.log(orderConfirmationDTO)
      confirmationOrder(orderConfirmationDTO).then(res=>{
        if(res.code === 200){
          proxy.$modal.msgSuccess(res.msg)
          open.value = false
          getList()
        }
      })
    }
  }else{
    open.value = false
  }
  // proxy.$refs["noticeRef"].validate(valid => {if (valid) {}})
}

/** 详情按钮操作 */
function handleDetail(row) {
  title.value = '订单详情'
  OrderNo.value = row.orderNo
  open.value = true
  nextTick(() => {
    detail.value.getDetail(row.orderNo)
  });
  /*proxy.$modal.confirm('是否确认删除公告编号为的数据项？').then(function () {
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})*/
}

/** 取消按钮 */
function cancel() {
  open.value = false
}

// 表单重置
/*function reset() {
  form.value = {}
  proxy.resetForm("noticeRef")
}*/

/** 查询公告列表 */
function getList() {
  noticeList.value = [
    {id:1,orderNo:'11111',repairUnit:'11111',equipment:'11111',brand:'11111',customerName:'11111',type:1,phone:'11111',description:'11111'},
    {id:2,orderNo:'22222',repairUnit:'22222',equipment:'22222',brand:'22222',customerName:'22222',type:3,phone:'22222',description:'22222'},
    {id:3,orderNo:'33333',repairUnit:'33333',equipment:'33333',brand:'33333',customerName:'33333',type:6,phone:'33333',description:'33333'},
  ]
  total.value = 3
  loading.value = false
  // loading.value = true
  /*getListOrders(queryParams.value).then(response => {
    noticeList.value = response.data.list
    total.value = response.data.total
    loading.value = false
  }).catch((err)=>{
    console.log(err)
    loading.value = false
  })*/
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

getList()
</script>
